<template>
  <a-layout class="main-layout">
    <!-- 头部 -->
    <a-layout-header class="header">
      <div class="header-left">
        <div class="title-group">
          <h1 class="site-title">大同AI赋能</h1>
          <span class="site-subtitle">让天下没有难做的生意</span>
        </div>
      </div>
      <div class="header-right">
        <a-button type="primary" class="create-btn" @click="$router.push('/workbench')">
          解锁超级工作台 >
        </a-button>
        <a-badge :count="notificationCount" :dot="notificationCount > 0">
          <a-button class="icon-btn" type="link" @click="showNotice">
            <a-icon type="bell" />
          </a-button>
        </a-badge>
        <template v-if="!isLoggedIn">
          <a-button type="link" class="login-btn" @click="$router.push('/auth/login')">
            登录
          </a-button>
        </template>
        <template v-else>
          <a-dropdown>
            <a-avatar :src="userAvatar" class="user-avatar">
              {{ userInitials }}
            </a-avatar>
            <template #overlay>
              <a-menu>
                <a-menu-item key="profile">
                  <router-link to="/user/profile">
                    <a-icon type="user" />个人信息
                  </router-link>
                </a-menu-item>
                <a-menu-item key="settings">
                  <router-link to="/user/account">
                    <a-icon type="setting" />账号设置
                  </router-link>
                </a-menu-item>
                <a-menu-divider />
                <a-menu-item key="logout" @click="handleLogout">
                  <a-icon type="logout" />退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </template>
      </div>
    </a-layout-header>

    <!-- 内容区 -->
    <a-layout-content class="content">
      <router-view></router-view>
    </a-layout-content>

    <!-- 底部导航 -->
    <div class="footer">
      <div class="footer-menu">
        <div v-for="tab in tabs" :key="tab.key" class="menu-item" :class="{ active: currentTab === tab.key }" @click="switchTab(tab.key)">
          <a-icon :type="tab.icon" />
          <span>{{ tab.label }}</span>
        </div>
      </div>
    </div>
  </a-layout>
</template>

<script>
import { Modal } from 'ant-design-vue';
import { mapGetters } from 'vuex'
import { getNotice } from '@/api/website'
export default {
  name: 'MainLayout',
  data() {
    return {
      currentTab: 'store',
      notificationCount: 0,
      notificationDetail: '',
      tabs: [
        { key: 'chat', icon: 'message', label: '对话聊天' },
        { key: 'store', icon: 'appstore', label: '应用市场' },
        { key: 'user', icon: 'user', label: '个人中心' }
      ]
    }
  },
  computed: {
    ...mapGetters({
      isLoggedIn: 'user/isLoggedIn',
      userAvatar: 'user/avatar',
      userNickname: 'user/nickName'
    }),
    userInitials() {
      return this.userNickname ? this.userNickname.charAt(0).toUpperCase() : ''
    }
  },  
  mounted() {
    if (this.isLoggedIn) {
      // 获取通知数量
      getNotice(encodeURIComponent(window.location.origin)).then(response => {
        this.notificationCount = response.data.count || 0;
        this.notificationDetail = response.data.detail || 0;
      })
    }
  },
  methods: {
    showNotice() { 
      if (this.isLoggedIn) {
        const h = this.$createElement;
        this.$info({
          title: '站点通知',
          content: h('div', {}, [
            h('p', this.notificationDetail),
          ]),
          onOk() { },
        });

      }
    },
    switchTab(tab) {
      this.currentTab = tab
      this.$router.push(`/${tab}`).catch(err => {
        if (err.name !== 'NavigationDuplicated') {
          throw err
        }
      })
    },
    async handleLogout() {
      try {
        await this.$store.dispatch('user/logout')
        this.$message.success('退出登录成功')
        this.$router.push('/auth/login')
      } catch (error) {
        this.$message.error('退出失败')
      }
    }
  },
  created() {
    const path = this.$route.path.split('/')[1] || 'store'
    this.currentTab = path
  }
}
</script>

<style lang="less" scoped>
.main-layout {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  background: #fff;
  padding: 0 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
  height: 56px;
  line-height: 56px;
  flex-shrink: 0;

  .header-left {
    display: flex;
    align-items: center;
    gap: 8px;

    .title-group {
      display: flex;
      align-items: center;
      gap: 8px;

      .site-title {
        margin: 0;
        font-size: 18px;
        color: #262626;
        font-weight: 500;
        white-space: nowrap;
      }

      .site-subtitle {
        font-size: 12px;
        color: #8c8c8c;
        white-space: nowrap;
        position: relative;
        padding-left: 8px;

        &::before {
          content: '';
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
          height: 14px;
          width: 1px;
          background-color: #e8e8e8;
        }
      }
    }
  }

  .header-right {
    display: flex;
    align-items: center;
    gap: 12px;

    .create-btn {
      border-radius: 20px;
      height: 32px;
      padding: 0 16px;
    }

    .icon-btn {
      padding: 4px 8px;
      height: 32px;

      .anticon {
        font-size: 16px;
      }
    }

    .login-btn {
      font-size: 14px;
      padding: 4px 15px;
      height: 32px;
    }

    .user-avatar {
      cursor: pointer;
      background-color: #1890ff;
      color: #fff;

      &:hover {
        opacity: 0.85;
      }
    }
  }
}

.content {
  flex: 1;
  overflow-y: auto;
  background: #f5f7fa;
  padding: 16px;
  padding-bottom: 76px;
}

.footer {
  height: 60px;
  background: #fff;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.06);
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;

  .footer-menu {
    height: 100%;
    display: flex;
    justify-content: space-around;
    align-items: center;

    .menu-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      gap: 4px;
      padding: 8px 0;
      cursor: pointer;
      transition: all 0.3s;
      flex: 1;

      .anticon {
        font-size: 24px;
        color: #999;
      }

      span {
        font-size: 12px;
        color: #999;
      }

      &.active {

        .anticon,
        span {
          color: #1890ff;
        }
      }

      &:hover {

        .anticon,
        span {
          color: #1890ff;
        }
      }
    }
  }
}

// 移动端适配
@media screen and (max-width: 768px) {
  .header {
    padding: 0 12px;
    height: 48px;
    line-height: 48px;

    .header-left {
      .title-group {
        gap: 6px;

        .site-title {
          font-size: 16px;
        }

        .site-subtitle {
          font-size: 11px;
          padding-left: 6px;

          &::before {
            height: 12px;
          }
        }
      }
    }

    .header-right {
      gap: 8px;

      .create-btn {
        font-size: 12px;
        padding: 0 12px;
        height: 28px;
      }

      .icon-btn {
        padding: 4px;
        height: 28px;
      }

      .login-btn {
        padding: 4px 8px;
        font-size: 12px;
        height: 28px;
      }

      .user-avatar {
        width: 28px;
        height: 28px;
        line-height: 28px;
        font-size: 14px;
      }
    }
  }
}

@media screen and (max-width: 375px) {
  .header {
    padding: 0 8px;

    .header-right {
      gap: 4px;
    }
  }
}
</style>